package cn.javacc.system.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import cn.javacc.core.database.BaseDao;
import cn.javacc.util.Status;

import cn.javacc.system.domain.User;

@Repository
public class UserDao extends BaseDao<User> {
     public UserDao() {
		super(User.class);
	}
	
    @SuppressWarnings("unchecked")
    public List<User> findAll(int offset, int fetchSize) {
        Query query = entityManager.createQuery("from User where status > 0");
        query.setFirstResult(offset);
        query.setMaxResults(fetchSize);
        return query.getResultList();
    }

    public int getTotalSize() {
        Query query = entityManager.createQuery("select count(user) from User user where user.status > 0");
        return ((Long) query.getSingleResult()).intValue();
    }

    public void delete(Long id) {
    	entityManager.createQuery("update User set status=?1,lastUpdateTime=?2 where id=?3").setParameter(1, Status.DELETE).setParameter(2, new Date()).setParameter(3, id).executeUpdate();
    }
}

